package com.example.forecast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.forecast.R;

import java.util.Random;

public class SixtyFourActivity extends AppCompatActivity {

    private Button start;//起卦按钮
    private TextView trigramName, trigramUp, trigramDown;
    private TextView trigramItem[] = new TextView[6];//爻名
    private ImageView trigram_img[] = new ImageView[6];//爻象
    private ImageView variable[] = new ImageView[6];//动爻标记
    private int trigramNum;
    private String upName, downName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sixty_four);

        start = findViewById(R.id.btn_sixtyFour_start);

        trigramName = findViewById(R.id.tv_sixtyFour_trigram_name);
        trigramUp = findViewById(R.id.tv_sixtyFour_trigram_up);
        trigramDown = findViewById(R.id.tv_sixtyFour_trigram_down);

        trigramItem[0] = findViewById(R.id.tv_sixtyFour_trigram1);
        trigramItem[1] = findViewById(R.id.tv_sixtyFour_trigram2);
        trigramItem[2] = findViewById(R.id.tv_sixtyFour_trigram3);
        trigramItem[3] = findViewById(R.id.tv_sixtyFour_trigram4);
        trigramItem[4] = findViewById(R.id.tv_sixtyFour_trigram5);
        trigramItem[5] = findViewById(R.id.tv_sixtyFour_trigram6);

        trigram_img[0] = findViewById(R.id.iv_sixtyFour_trigram_img1);
        trigram_img[1] = findViewById(R.id.iv_sixtyFour_trigram_img2);
        trigram_img[2] = findViewById(R.id.iv_sixtyFour_trigram_img3);
        trigram_img[3] = findViewById(R.id.iv_sixtyFour_trigram_img4);
        trigram_img[4] = findViewById(R.id.iv_sixtyFour_trigram_img5);
        trigram_img[5] = findViewById(R.id.iv_sixtyFour_trigram_img6);

        variable[0] = findViewById(R.id.iv_sixtyFour_variable1);
        variable[1] = findViewById(R.id.iv_sixtyFour_variable2);
        variable[2] = findViewById(R.id.iv_sixtyFour_variable3);
        variable[3] = findViewById(R.id.iv_sixtyFour_variable4);
        variable[4] = findViewById(R.id.iv_sixtyFour_variable5);
        variable[5] = findViewById(R.id.iv_sixtyFour_variable6);

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int trigram[] = new int[6];
                for (int i = 0; i < 6; i++) {
                    int temp = arithmetic();

                    switch (temp) {
                        case -1:
                            trigramItem[i].setText(siteJudge(-1, i));
                            trigram_img[i].setImageResource(R.drawable.yin_hexagram);
                            variable[i].setImageResource(R.drawable.variable);
                            trigram[i] = 0;
                            break;
                        case 0:
                            trigramItem[i].setText(siteJudge(0, i));
                            trigram_img[i].setImageResource(R.drawable.yin_hexagram);
                            variable[i].setImageResource(R.drawable.stabilize);
                            trigram[i] = 0;
                            break;
                        case 1:
                            trigramItem[i].setText(siteJudge(1, i));
                            trigram_img[i].setImageResource(R.drawable.yang_hexagram);
                            variable[i].setImageResource(R.drawable.stabilize);
                            trigram[i] = 1;
                            break;
                        case 2:
                            trigramItem[i].setText(siteJudge(2, i));
                            trigram_img[i].setImageResource(R.drawable.yang_hexagram);
                            variable[i].setImageResource(R.drawable.variable);
                            trigram[i] = 1;
                            break;
                    }
                }
                String str = confirmTrigram(trigramName(trigram));//卦名
                String upStr = upName.substring(upName.length() - 1);//上卦属性
                String downStr = downName.substring(downName.length() - 1);//下卦属性
                if (upStr.equals(downStr)) {//判断上下卦是否一样
                    StringBuffer stringBuffer = new StringBuffer(str);
                    trigramName.setText(stringBuffer.insert(1,upStr));//插入字符串获取完整卦名并设置
                } else {
                    trigramName.setText(upStr + downStr + str);//拼接完整卦名并设置
                }
                trigramUp.setText("上卦：" + upName);
                trigramDown.setText("下卦：" + downName);
            }
        });


    }

    /**
     * @author: 日月同珲
     * @time: 2021/10/23 20:58
     * @param: null
     * @effect: 取爻
     * @return: int result(爻象) -1为动阴，0为阴，1为阳，2为动阳
     */
    private int arithmetic() {

        Random random = new Random();

        int result;//-1为动阴，0为阴，1为阳，2为动阳
        int yin = 0;
        int yang = 0;

        for (int i = 0; i < 3; i++) {
            int temp = random.nextInt(2);
            if (temp == 1) {
                yang++;
            } else {
                yin++;
            }
        }
        if (yang > yin) {
            if (yang == 3) {
                result = 2;
            } else {
                result = 1;
            }
        } else {
            if (yin == 3) {
                result = -1;
            } else {
                result = 0;
            }
        }


        return result;
    }

    /**
     * @author: 日月同珲
     * @time: 2021/10/23 21:00
     * @param: int attribute(爻象)，site(爻位)
     * @effect: 获取爻名
     * @return: String siteName(爻名)
     */
    private String siteJudge(int attribute, int site) {

        String siteName = "";

        if (attribute > 0) {
            switch (site) {
                case 0:
                    siteName = "初九";
                    break;
                case 1:
                    siteName = "九二";
                    break;
                case 2:
                    siteName = "九三";
                    break;
                case 3:
                    siteName = "九四";
                    break;
                case 4:
                    siteName = "九五";
                    break;
                case 5:
                    siteName = "上九";
                    break;
            }
        } else {
            switch (site) {
                case 0:
                    siteName = "初六";
                    break;
                case 1:
                    siteName = "六二";
                    break;
                case 2:
                    siteName = "六三";
                    break;
                case 3:
                    siteName = "六四";
                    break;
                case 4:
                    siteName = "六五";
                    break;
                case 5:
                    siteName = "上六";
                    break;
            }
        }


        return siteName;
    }

    /**
     * @author: 日月同珲
     * @time: 2021/10/23 22:17
     * @param: int[] trigram(六爻)
     * @effect: 获取上下两卦的卦名
     * @return: int[] trigramName(卦名数组)
     */
    private int[] trigramName(int[] trigram) {//先天八卦：0乾、1坤、2震、3巽、4坎、5离、6艮、7兑
        //后天八卦：0乾、1坎、2艮、3震、4巽、5离、6坤、7兑(此处采用后天八卦)
        int trigramName[] = new int[2];

        if (trigram[0] == 0) {
            if (trigram[1] == 0) {
                if (trigram[2] == 0) {
                    trigramName[1] = 6;
                    downName = "坤为地";
                } else {
                    trigramName[1] = 2;
                    downName = "艮为山";
                }
            } else {
                if (trigram[2] == 0) {
                    trigramName[1] = 1;
                    downName = "坎为水";
                } else {
                    trigramName[1] = 4;
                    downName = "巽为风";
                }
            }
        } else {
            if (trigram[1] == 1) {
                if (trigram[2] == 1) {
                    trigramName[1] = 0;
                    downName = "乾为天";
                } else {
                    trigramName[1] = 7;
                    downName = "兑为泽";
                }
            } else {
                if (trigram[2] == 1) {
                    trigramName[1] = 5;
                    downName = "离为火";
                } else {
                    trigramName[1] = 3;
                    downName = "震为雷";
                }
            }
        }

        if (trigram[3] == 0) {
            if (trigram[4] == 0) {
                if (trigram[5] == 0) {
                    trigramName[0] = 6;
                    upName = "坤为地";
                } else {
                    trigramName[0] = 2;
                    upName = "艮为山";
                }
            } else {
                if (trigram[5] == 0) {
                    trigramName[0] = 1;
                    upName = "坎为水";
                } else {
                    trigramName[0] = 4;
                    upName = "巽为风";
                }
            }
        } else {
            if (trigram[4] == 1) {
                if (trigram[5] == 1) {
                    trigramName[0] = 0;
                    upName = "乾为天";
                } else {
                    trigramName[0] = 7;
                    upName = "兑为泽";
                }
            } else {
                if (trigram[5] == 1) {
                    trigramName[0] = 5;
                    upName = "离为火";
                } else {
                    trigramName[0] = 3;
                    upName = "震为雷";
                }
            }
        }

        return trigramName;
    }

    private String confirmTrigram(int trigramArray[]) {
        String trigramName = "";

        switch (trigramArray[0]) {//0乾、1坎、2艮、3震、4巽、5离、6坤、7兑
            case 0:
                switch (trigramArray[1]) {
                    case 0:
                        trigramName = "乾卦";
                        trigramNum = 1;
                        break;
                    case 1:
                        trigramName = "讼卦";
                        trigramNum = 6;
                        break;
                    case 2:
                        trigramName = "遁卦";
                        trigramNum = 33;
                        break;
                    case 3:
                        trigramName = "无妄卦";
                        trigramNum = 25;
                        break;
                    case 4:
                        trigramName = "姤卦";
                        trigramNum = 44;
                        break;
                    case 5:
                        trigramName = "同人卦";
                        trigramNum = 13;
                        break;
                    case 6:
                        trigramName = "否卦";
                        trigramNum = 12;
                        break;
                    case 7:
                        trigramName = "履卦";
                        trigramNum = 10;
                        break;
                }
                break;
            case 1:
                switch (trigramArray[1]) {//0乾、1坎、2艮、3震、4巽、5离、6坤、7兑
                    case 0:
                        trigramName = "需卦";
                        trigramNum = 5;
                        break;
                    case 1:
                        trigramName = "坎卦";
                        trigramNum = 29;
                        break;
                    case 2:
                        trigramName = "蹇卦";
                        trigramNum = 39;
                        break;
                    case 3:
                        trigramName = "屯卦";
                        trigramNum = 3;
                        break;
                    case 4:
                        trigramName = "井卦";
                        trigramNum = 48;
                        break;
                    case 5:
                        trigramName = "即济卦";
                        trigramNum = 63;
                        break;
                    case 6:
                        trigramName = "比卦";
                        trigramNum = 8;
                        break;
                    case 7:
                        trigramName = "节卦";
                        trigramNum = 60;
                        break;
                }
                break;
            case 2://0乾、1坎、2艮、3震、4巽、5离、6坤、7兑
                switch (trigramArray[1]) {//0乾、1坎、2艮、3震、4巽、5离、6坤、7兑
                    case 0:
                        trigramName = "大畜卦";
                        trigramNum = 26;
                        break;
                    case 1:
                        trigramName = "蒙卦";
                        trigramNum = 4;
                        break;
                    case 2:
                        trigramName = "艮卦";
                        trigramNum = 52;
                        break;
                    case 3:
                        trigramName = "颐卦";
                        trigramNum = 27;
                        break;
                    case 4:
                        trigramName = "蛊卦";
                        trigramNum = 18;
                        break;
                    case 5:
                        trigramName = "贲卦";
                        trigramNum = 22;
                        break;
                    case 6:
                        trigramName = "剥卦";
                        trigramNum = 8;
                        break;
                    case 7:
                        trigramName = "损卦";
                        trigramNum = 41;
                        break;
                }
                break;
            case 3:
                switch (trigramArray[1]) {//0乾、1坎、2艮、3震、4巽、5离、6坤、7兑
                    case 0:
                        trigramName = "大壮卦";
                        trigramNum = 34;
                        break;
                    case 1:
                        trigramName = "解卦";
                        trigramNum = 40;
                        break;
                    case 2:
                        trigramName = "小过卦";
                        trigramNum = 62;
                        break;
                    case 3:
                        trigramName = "震卦";
                        trigramNum = 51;
                        break;
                    case 4:
                        trigramName = "恒卦";
                        trigramNum = 32;
                        break;
                    case 5:
                        trigramName = "丰卦";
                        trigramNum = 55;
                        break;
                    case 6:
                        trigramName = "豫卦";
                        trigramNum = 16;
                        break;
                    case 7:
                        trigramName = "归妹卦";
                        trigramNum = 54;
                        break;
                }
                break;
            case 4:
                switch (trigramArray[1]) {//0乾、1坎、2艮、3震、4巽、5离、6坤、7兑
                    case 0:
                        trigramName = "小畜卦";
                        trigramNum = 9;
                        break;
                    case 1:
                        trigramName = "涣卦";
                        trigramNum = 59;
                        break;
                    case 2:
                        trigramName = "渐卦";
                        trigramNum = 53;
                        break;
                    case 3:
                        trigramName = "益卦";
                        trigramNum = 42;
                        break;
                    case 4:
                        trigramName = "巽卦";
                        trigramNum = 57;
                        break;
                    case 5:
                        trigramName = "家人卦";
                        trigramNum = 37;
                        break;
                    case 6:
                        trigramName = "观卦";
                        trigramNum = 20;
                        break;
                    case 7:
                        trigramName = "中孚卦";
                        trigramNum = 61;
                        break;
                }
                break;
            case 5:
                switch (trigramArray[1]) {//0乾、1坎、2艮、3震、4巽、5离、6坤、7兑
                    case 0:
                        trigramName = "大有卦";
                        trigramNum = 14;
                        break;
                    case 1:
                        trigramName = "未济卦";
                        trigramNum = 64;
                        break;
                    case 2:
                        trigramName = "旅卦";
                        trigramNum = 56;
                        break;
                    case 3:
                        trigramName = "噬嗑卦";
                        trigramNum = 21;
                        break;
                    case 4:
                        trigramName = "鼎卦";
                        trigramNum = 50;
                        break;
                    case 5:
                        trigramName = "离卦";
                        trigramNum = 30;
                        break;
                    case 6:
                        trigramName = "晋卦";
                        trigramNum = 35;
                        break;
                    case 7:
                        trigramName = "睽卦";
                        trigramNum = 38;
                        break;
                }
                break;
            case 6:
                switch (trigramArray[1]) {//0乾、1坎、2艮、3震、4巽、5离、6坤、7兑
                    case 0:
                        trigramName = "泰卦";
                        trigramNum = 11;
                        break;
                    case 1:
                        trigramName = "师卦";
                        trigramNum = 7;
                        break;
                    case 2:
                        trigramName = "谦卦";
                        trigramNum = 15;
                        break;
                    case 3:
                        trigramName = "复卦";
                        trigramNum = 24;
                        break;
                    case 4:
                        trigramName = "升卦";
                        trigramNum = 46;
                        break;
                    case 5:
                        trigramName = "明夷卦";
                        trigramNum = 36;
                        break;
                    case 6:
                        trigramName = "坤卦";
                        trigramNum = 2;
                        break;
                    case 7:
                        trigramName = "临卦";
                        trigramNum = 19;
                        break;
                }
                break;
            case 7:
                switch (trigramArray[1]) {//0乾、1坎、2艮、3震、4巽、5离、6坤、7兑
                    case 0:
                        trigramName = "夬卦";
                        trigramNum = 43;
                        break;
                    case 1:
                        trigramName = "困卦";
                        trigramNum = 47;
                        break;
                    case 2:
                        trigramName = "咸卦";
                        trigramNum = 31;
                        break;
                    case 3:
                        trigramName = "随卦";
                        trigramNum = 17;
                        break;
                    case 4:
                        trigramName = "大过卦";
                        trigramNum = 28;
                        break;
                    case 5:
                        trigramName = "革卦";
                        trigramNum = 49;
                        break;
                    case 6:
                        trigramName = "萃卦";
                        trigramNum = 45;
                        break;
                    case 7:
                        trigramName = "兑卦";
                        trigramNum = 58;
                        break;
                }
                break;
        }

        return trigramName;
    }

}